import { runApp, IAppConfig } from 'ice';
import request from './request';
import 'windi-utilities.css';

import { ConfigProvider, message } from 'antd';
import { CustomStyleProvider, ConfigProviderProps } from './theme';
import { flatJSONTree } from './utils/json';

import ErrorPage from './components/ErrorPage';

// 构造 APP 配置
const appConfig: IAppConfig = {
  app: {
    rootId: 'ice-container',
    addProvider({ children }) {
      return (
        <ConfigProvider {...ConfigProviderProps}>
          <CustomStyleProvider>{children}</CustomStyleProvider>
        </ConfigProvider>
      );
    },
    async getInitialData() {
      // 从 localStorage 中获取用户信息
      const userInfos = window.localStorage.getItem('useinfos');

      return {
        // 其他的初始化配置（全局目录配置）
        SceneConfig: flatJSONTree(window.SceneConfig),
        // 传递给 store 初始化值
        initialStates: {
          user: userInfos ? JSON.parse(userInfos) : undefined,
        },
      };
    },
  },
  request,
  router: {
    type: 'hash',
  },
  auth: {
    NoAuthFallback: <ErrorPage code={403} text="抱歉，你无权访问该页面" />,
  },
};

runApp(appConfig);
